package com.younio.local;

import com.younio.local.compnent.SpringContextHolder;
import com.younio.local.compnent.datasource.MultiDataSourceHolder;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.socket.config.annotation.EnableWebSocket;

import javax.swing.*;
import java.awt.*;

@MapperScan("com.younio.local.repository")
@SpringBootApplication
@EnableWebSocket
public class LocalApplication {

    //false 停止  true 启动
    private static boolean running;
    //是否可操作
    private static boolean allowOps;

    private static JTextField textField = new JTextField();

    public static void main(String[] args) {
        if (System.getProperty("os.name").toLowerCase().contains("win")) {
            allowOps = true;
            Frame main = new Frame("RFID信息系统-校表启动程序");
            GridLayout gridLayout = new GridLayout(2, 1);
            main.setLayout(gridLayout);

            Button start = new Button("启动");
            start.setFont(new Font("宋体", Font.BOLD, 24));
            Button stop = new Button("停止");
            stop.setFont(new Font("宋体", Font.BOLD, 24));

            Button exit = new Button("退出");
            exit.setFont(new Font("宋体", Font.BOLD, 24));

            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridLayout(1, 3));
            buttonPanel.add(start);
            buttonPanel.add(stop);
            buttonPanel.add(exit);
            main.add(buttonPanel);

            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(1, 3));
            textField.setHorizontalAlignment(0);
            textField.setText("未启动");
            textField.setFont(new Font("宋体", Font.BOLD, 28));

            panel.add(textField);
            main.add(panel);

            main.setBounds(400, 400, 800, 300);
            main.setVisible(true);
            start.addActionListener(e -> start(main, args));
            stop.addActionListener(e -> stop(main));
            exit.addActionListener(e -> {
                stop(main);
                System.exit(0);
            });
        } else {
            doStart(args);
        }


    }

    private static void dialog(Frame frame, String msg) {
        Dialog dialog = new Dialog(frame, "警告", true);
        dialog.setBounds(400, 400, 200, 100);
        dialog.setTitle("警告");
        dialog.setLayout(new FlowLayout());
        dialog.add(new Label(msg));

        Button button = new Button("确定");
        button.addActionListener(e -> dialog.setVisible(false));
        dialog.add(button);
        dialog.setVisible(true);
    }

    private static void start(Frame frame, String[] args) {
        if (running) {
            dialog(frame, "程序已经启动");
        } else if (!allowOps) {
            dialog(frame, "正在执行其他操作，请稍后");
        } else {
            allowOps = false;
            doStart(args);
        }
    }

    private static void stop(Frame frame) {
        if (!running) {
            dialog(frame, "程序尚未启动");
        } else if (!allowOps) {
            dialog(frame, "正在执行其他操作，请稍后");
        } else {
            allowOps = false;
            doStop();
            running = false;
            allowOps = true;
        }
    }

    private static void doStop() {
        setLabel("正在停止");
        ApplicationContext applicationContext = SpringContextHolder.getApplicationContext();
        MultiDataSourceHolder.clear();
        ((ConfigurableApplicationContext) applicationContext).close();
        setLabel("已停止");
    }

    private static void doStart(String[] args) {
        setLabel("开始启动");
        Thread mainThread = new Thread(() -> {
            SpringApplication application = new SpringApplication(LocalApplication.class);
            ApplicationPidFileWriter pidFileWriter = new ApplicationPidFileWriter();
            application.addListeners(pidFileWriter);
            application.run(args);
            setLabel("启动成功");
            running = true;
            allowOps = true;
        });
        mainThread.start();
    }

    public static void setLabel(String label) {
        textField.setText(label);
    }
}